/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// The base class of chart. All chart relative classes derived from this
	/// </summary>
	[Serializable]
	public abstract class EQChart: EQGraphic  	
	{
		private Excel.XlChartType mChartType;
		private string mChartTitle = "";
		private EQBool mIsShowLegend = EQBool.Default;
		private EQBool mIsShowLegendKey = EQBool.Default;
		private EQBool mIsAutoText = EQBool.Default;
		private EQBool mIsShowCategoryName = EQBool.Default;
		private EQTableBase mChartSource = null;
		private Excel.XlDataLabelsType mDataLabelsType = Excel.XlDataLabelsType.xlDataLabelsShowLabel;

		/// <summary>
		/// Protected constructor
		/// </summary>
		protected EQChart(){}

		/// <summary>
		/// Set/Get source of chart
		/// </summary>
		protected EQTableBase ChartSource
		{
			get
			{
				return this.mChartSource;
			}
			set
			{
				this.mChartSource = value;
			}
		}

		/// <summary>
		/// Set/Get type of chart. It's the type provided from Excel COM. Pay attention that you have refered excel.dll file into this project
		/// </summary>
		protected Excel.XlChartType ChartType
		{
			set
			{
				this.mChartType = value;
			}
			get
			{
				return this.mChartType;
			}
		}

		/// <summary>
		/// Set/Get the chart title
		/// </summary>
		public string ChartTitle
		{
			set
			{
				mChartTitle = value;
			}
			get
			{
				return mChartTitle;
			}
		}

		/// <summary>
		/// Set/Get whether show the guide line on chart or series
		/// </summary>
		public bool IsShowLegend
		{
			set
			{
				mIsShowLegend = EQKit.ToEQBool(value);
			}
			get
			{
				return EQKit.ToBool(mIsShowLegend);
			}
		}

		/// <summary>
		/// Set/Get Whether show designation near the data, default value is false 
		/// </summary>
		public bool IsShowLegendKey
		{
			set
			{
				mIsShowLegendKey = EQKit.ToEQBool(value);
			}
			get
			{
				return EQKit.ToBool(mIsShowLegendKey);
			}
		}

		/// <summary>
		/// Set/Get whether create correct text based on the datasource of chart, default value is false
		/// </summary>
		public bool IsAutoText
		{
			set
			{
				mIsAutoText = EQKit.ToEQBool(value);
			}
			get
			{
				return EQKit.ToBool(mIsAutoText);
			}
		}

		/// <summary>
		/// Set/Get whether show title of each value
		/// </summary>
		public bool IsShowCategoryName
		{
			set
			{
				mIsShowCategoryName = EQKit.ToEQBool(value);
			}
			get
			{
				return EQKit.ToBool(mIsShowCategoryName);
			}
		}

		/// <summary>
		/// Set/Get the data labels type of chart
		/// </summary>
		public EQDataLabelsType DataLabelsType
		{
			get
			{
				return EQEnumKit.ToEQDataLabelsType(this.mDataLabelsType);
			}
			set
			{
				this.mDataLabelsType = EQEnumKit.ToXlDataLabelsType(value);
			}
		}

		/// <summary>
		/// Bind a EQTableBase object to this
		/// </summary>
		/// <param name="table"></param>
		protected internal virtual void RenderTableBase(EQTableBase table)
		{
			this.mChartSource = table;
		}

		/// <summary>
		/// Initialize chart object and return Excel.Chart COM interface
		/// </summary>
		/// <param name="objWorksheet"></param>
		/// <returns></returns>
		protected internal Excel.Chart InitializeChart(Excel.Worksheet objWorksheet)
		{
			Excel.Chart objChart = (Excel.Chart)objWorksheet.Application.ActiveWorkbook.Charts.Add(this.Missing, this.Missing, this.Missing, this.Missing);
			try
			{
				objChart.ChartType = this.ChartType;
			}
			catch
			{
				throw new EQException("EQ1032", new string[]{this.ChartType.ToString()});
			}
			objChart.Activate();
			if(this.ChartSource is EQPivotTable)
			{
				Excel.Range objRange = (Excel.Range)objWorksheet.Cells[this.ChartSource.TableTop, this.ChartSource.TableLeft];
				objChart.SetSourceData(objRange, this.Missing);
			}
			return objChart;
		}

		/// <summary>
		/// Output current object to excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			base.Output(objWorksheet);

			Excel.Chart objChart = objWorksheet.Application.ActiveChart;
			Excel.Series objSeries = (Excel.Series)objChart.SeriesCollection(1);
			//objSeries.ApplyDataLabels(this.mDataLabelsType, this.IsShowLegendKey, this.IsAutoText, this.Missing);
			objChart.Location(Excel.XlChartLocation.xlLocationAsObject, objWorksheet.Name);

			Excel.ChartObject objChartObject =(Excel.ChartObject)objWorksheet.ChartObjects(1);
			
			objChartObject.Chart.HasTitle = true;
			objChartObject.Chart.ChartTitle.Text = this.ChartTitle; 
			objChartObject.Chart.PlotArea.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone;
			objChartObject.Chart.PlotArea.Border.Weight = Excel.XlBorderWeight.xlThin;
			objChartObject.Chart.PlotArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;

			Excel.Range objRange = (Excel.Range)objWorksheet.Cells[this.Top, this.Left];	//Get the relative cell
			double left = double.Parse(objRange.Left.ToString(), EQKit.ObjNumberFormatInfo);	//Convert column index to pixel
			double top = double.Parse(objRange.Top.ToString(), EQKit.ObjNumberFormatInfo);;		//Convert row index to pixel

			objChartObject.Left = left;
			objChartObject.Top = top;
			objChartObject.Width = this.Width;
			objChartObject.Height = this.Height;
		}
	}
}
